package com.alpha.eceasy.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alpha.eceasy.extension.BaseFormInfo;
import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.mall.api.dto.MallCommodityDTO;
import com.alpha.eceasy.mall.api.entity.MallCommodity;
import com.alpha.eceasy.mall.api.entity.MallCommodityCategoryAsso;
import com.alpha.eceasy.mall.api.entity.MallCommodityDetail;
import com.alpha.eceasy.exception.BizException;
import com.alpha.eceasy.mall.api.entity.MallCommoditySpecification;
import com.alpha.eceasy.mall.api.vo.MallCommodityVO;
import com.alpha.eceasy.mall.mapper.MallCommodityCategoryAssoMapper;
import com.alpha.eceasy.mall.mapper.MallCommodityMapper;
import com.alpha.eceasy.mall.service.MallCommodityCategoryService;
import com.alpha.eceasy.mall.service.MallCommodityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
@Log4j2
@RequiredArgsConstructor
public class MallCommodityServiceImpl extends BaseServiceImpl<MallCommodityMapper, MallCommodity>
        implements MallCommodityService {
    @Resource
    private MallCommodityDetailServiceImpl commodityDetailService;
    @Resource
    private MallCommodityCategoryAssoMapper commodityCategoryAssoMapper;
    @Resource
    private MallCommodityCategoryService commodityCategoryService;
    @Resource
    private MallCommoditySpecificationServiceImpl commoditySpecificationService;

    @Override
    public IPage<MallCommodityVO> getMallCommodityPage(Page<MallCommodity> page, MallCommodity entity) {
        return this.page(page, Wrappers.<MallCommodity>lambdaQuery()
                        .like(StrUtil.isNotBlank(entity.getCommodityName()),
                                MallCommodity::getCommodityName, entity.getCommodityName())
                        .eq(Objects.nonNull(entity.getState()),
                                MallCommodity::getState, entity.getState())
                        .orderByDesc(BaseFormInfo::getCreateTime))
                .convert(commodity -> this.getByGuid(commodity.getGuid()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String save(MallCommodityDTO dto) {
        MallCommodity commodity = new MallCommodity(dto);
        if (!super.save(commodity)) {
            throw new BizException("新增商品失败");
        }
        String commodityGuid = commodity.getGuid();
        MallCommodityDetail commodityDetail = new MallCommodityDetail(dto);
        commodityDetail.setCommodityGuid(commodityGuid);
        if (!commodityDetailService.save(commodityDetail)) {
            throw new BizException("新增商品详情失败");
        }
        MallCommodityCategoryAsso asso = new MallCommodityCategoryAsso();
        asso.setCommodityGuid(commodityGuid);
        asso.setCommodityCategoryGuid(dto.getCommodityCategoryGuid());
        if (!SqlHelper.retBool(commodityCategoryAssoMapper.insertByGuid(asso))) {
            throw new BizException("新增商品与品类的关联失败");
        }
        return commodityGuid;
    }

    @Override
    public MallCommodityVO getByGuid(Serializable guid) {
        MallCommodityDetail commodityDetail = commodityDetailService.getByCommodityGuid(guid);
        List<MallCommodityCategoryAsso> commodityCategoryAssoList = commodityCategoryAssoMapper.selectList(Wrappers.<MallCommodityCategoryAsso>lambdaQuery()
                .eq(MallCommodityCategoryAsso::getCommodityGuid, guid));
        MallCommodityVO vo = new MallCommodityVO(super.getByGuid(guid), commodityDetail);
        List<String> categoryGuidList = commodityCategoryAssoList.stream().map(MallCommodityCategoryAsso::getCommodityCategoryGuid).toList();
        vo.setCommodityCategoryGuidList(categoryGuidList);
        vo.setCommodityCategoryNameList(categoryGuidList.stream()
                .map(categoryGuid -> commodityCategoryService.getByGuid(categoryGuid).getName())
                .toList());
        Optional.ofNullable(commoditySpecificationService.list(Wrappers.<MallCommoditySpecification>lambdaQuery()
                        .eq(MallCommoditySpecification::getCommodityGuid, guid)))
                .ifPresent(vo::setCommoditySpecificationList);
        return vo;
    }
}
